package com.indexingsystem.boss.utils;

import com.indexingsystem.boss.Vo.ExportThemeWordsResVo;
import com.indexingsystem.boss.Vo.RelationThemeWordsResVo;
import com.indexingsystem.boss.Vo.ThemeTableResVo;
import com.indexingsystem.boss.Vo.ThemeWordsResVo;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProductIndexingExcelUtil extends ExcelUtil {

    private String[] heads = {"主题词表id", "主题词表名", "主题词表编号", "主题词id", "主题词名", "主题词编号", "主题词外部标引", "相关主题词id", "相关主题词", "相关主题词编号"};

    public void ExportWbToFile(HSSFWorkbook wb, String filePath, String productId) throws IOException {
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        String dateFilePathStr = df2.format(new Date());
        String dateFileStr = df1.format(new Date());
        String exportPath = null;
        if (filePath.endsWith("\\")) {
            exportPath = filePath + dateFilePathStr;
        } else {
            exportPath = filePath + "\\" + dateFilePathStr;
        }
        File file = new File(exportPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        FileOutputStream out = new FileOutputStream(exportPath + "\\" + productId + dateFileStr + ".xls");
        wb.write(out);
        out.flush();
        out.close();
    }

    public HSSFWorkbook doExportXLS(ExportThemeWordsResVo data, String sheetname) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetname);
        setStringValue(sheet, 0, 0, "productid");
        setStringValue(sheet, 1, 0, "name");
        setStringValue(sheet, 2, 0, "exporttime");
        setStringValue(sheet, 3, 0, "exporttype");

        doSetCell(wb, sheet, 0, 1, data.getProductId());
        doSetCell(wb, sheet, 1, 1, data.getProductName());
        doSetCell(wb, sheet, 2, 1, data.getExportType());
        doSetCell(wb, sheet, 3, 1, data.getExportTime());
        createXLSHeader(sheet);
        int row = 5;
        List<ThemeTableResVo> tables = data.getTables();
        if (tables != null && tables.size() > 0) {
            for (ThemeTableResVo t : tables) {
                row = addThemeTable(wb, sheet, row, t);
            }
        }

        return wb;
    }


    public void createXLSHeader(HSSFSheet sheet) {
        for (int i = 0; i < heads.length; i++) {
            setStringValue(sheet, (short) 4, (short) i, heads[i]);
        }
    }

    /**
     * 添加主题词表格
     *
     * @param wb
     * @param sheet
     * @param row
     * @param data
     * @return
     */
    public int addThemeTable(HSSFWorkbook wb, HSSFSheet sheet, int row, ThemeTableResVo data) {

        this.doSetCell(wb, sheet, (short) row, (short) 0, data.getThemeTableId());
        this.doSetCell(wb, sheet, (short) row, (short) 1, data.getThemeTableName());
        this.doSetCell(wb, sheet, (short) row, (short) 2, data.getThemeTableCode());
        List<ThemeWordsResVo> relations = data.getThemeWords();
        if (relations != null && relations.size() > 0) {
            for (ThemeWordsResVo r : relations) {
                row = addThemeWord(wb, sheet, row, r);
            }
        } else {
            row++;
        }

        return row;
    }

    /**
     * 添加主题词到excel
     *
     * @param wb
     * @param sheet
     * @param row
     * @param data
     * @return
     */
    public int addThemeWord(HSSFWorkbook wb, HSSFSheet sheet, int row, ThemeWordsResVo data) {

        this.doSetCell(wb, sheet, (short) row, (short) 3, data.getThemeWordsId());
        this.doSetCell(wb, sheet, (short) row, (short) 4, data.getThemeWordsName());
        this.doSetCell(wb, sheet, (short) row, (short) 5, data.getThemeWordsCode());
        this.doSetCell(wb, sheet, (short) row, (short) 6, data.getExtraIndexing());
        List<RelationThemeWordsResVo> relations = data.getRelations();
        if (relations != null && relations.size() > 0) {
            for (RelationThemeWordsResVo r : relations) {
                addRelationThemeWord(wb, sheet, row, r);
                row++;
            }
        } else {
            row++;
        }

        return row;
    }

    /**
     * 添加相关主题词
     *
     * @param wb
     * @param sheet
     * @param row
     * @param data
     */
    public void addRelationThemeWord(HSSFWorkbook wb, HSSFSheet sheet, int row, RelationThemeWordsResVo data) {

        this.doSetCell(wb, sheet, (short) row, (short) 7, data.getThemeWordsId());
        this.doSetCell(wb, sheet, (short) row, (short) 8, data.getThemeWordsName());
        this.doSetCell(wb, sheet, (short) row, (short) 9, data.getThemeWordsCode());
    }


    /**
     * 获取导出文件的数据部分的总行数
     * @param data
     * @return
     */
    /*public int getDataRowsNumber(ExportThemeWordsResVo data) {
        int count = 0;
        List<ThemeTableResVo> tables = data.getTables();
        if (tables != null && tables.size() > 0) {
            for (ThemeTableResVo t : tables) {
                count += 1;
                List<ThemeWordsResVo> themeWords = t.getThemeWords();
                if (themeWords != null && themeWords.size() > 0) {
                    count += themeWords.size()-1;
                    for (ThemeWordsResVo w : themeWords) {
                        if (w.getRelations() != null && w.getRelations().size() > 0) {
                            count += w.getRelations().size()-1;
                        }
                    }
                }
            }
        }
        LogCvt.info("***************总行数*******************：" + count);
        return count;
    }*/
}
